library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.2     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(knitr)
library(cowplot)
library(latex2exp)
## Warning: package 'latex2exp' was built under R version 4.0.5
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:latex2exp':
## 
##     TeX
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gapminder)
library("png")
library("RCurl")
## Warning: package 'RCurl' was built under R version 4.0.5
## 
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
## 
##     complete
library("gifski")
library("grDevices")
library("gganimate")
library("ggridges")
#reading in data sets
county_elect <- read_csv("countypresidential_election_2000-2020.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   year = col_double(),
##   state = col_character(),
##   state_po = col_character(),
##   county_name = col_character(),
##   county_fips = col_double(),
##   office = col_character(),
##   candidate = col_character(),
##   party = col_character(),
##   candidatevotes = col_double(),
##   totalvotes = col_double(),
##   version = col_double(),
##   mode = col_character()
## )
geo_code <- read_csv("fips2geocode.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   fips = col_double(),
##   county = col_character(),
##   state = col_character(),
##   lon = col_double(),
##   lat = col_double(),
##   rMapState = col_character(),
##   rMapCounty = col_character()
## )
county_elect_2020<- county_elect %>% filter(year=="2020" , party =='DEMOCRAT' | party == "REPUBLICAN")
head(geo_code)
head(county_elect_2020)
names(county_elect_2020)[names(county_elect_2020) == "county_fips"] <- "fips"
head(county_elect_2020)

Merge the two data sets

merged <- merge(county_elect_2020, geo_code, by='fips', all.x=TRUE)
head(merged)

selecting only the winner from the county by selecting the line/candidate with the max amount of candidate votes

group <- data.frame(merged)
# sorting the sub and marks.
sorted_group <- group[order(group$county, -group$candidatevotes),]
# removing duplicates from the sorted sub column
ans <- sorted_group[!duplicated(sorted_group$county),]
ans
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.5
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.5
## Loading required package: sp
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-29, (SVN revision 1165M)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.0, released 2021/11/04
## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 8.1.1, September 1st, 2021, [PJ_VERSION: 811]
## Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
library(geojsonio)
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
library(htmltools)
library(htmlwidgets)
library(stringi)
library(RColorBrewer)
m <- leaflet() %>%
  addProviderTiles(providers$CartoDB.PositronNoLabels)  %>%
  # center of US based on the geo code.
  setView(lng = -96.25, lat = 39.50, zoom = 4)  
m
ans$fips %>% as.character(ans$fips)
##    [1] "45001" "22001" "51001" "16001" "21001" "8001"  "50001" "45003" "27001"
##   [10] "12001" "37001" "6001"  "8003"  "36001" "51003" "26001" "28003" "37003"
##   [19] "40003" "26003" "19005" "26005" "36003" "51005" "42003" "18003" "22003"
##   [28] "45005" "26007" "6003"  "6005"  "51007" "51009" "28005" "45007" "29003"
##   [37] "48003" "23001" "48005" "24003" "27003" "37007" "31003" "26009" "4001" 
##   [46] "19007" "13001" "51011" "48007" "8005"  "48009" "8007"  "26011" "5001" 
##   [55] "51013" "42005" "23003" "31005" "22005" "37009" "39005" "5003"  "39007"
##   [64] "53003" "22007" "48013" "20005" "39009" "13003" "34001" "40005" "28007"
##   [73] "29007" "19009" "39011" "51015" "46003" "48015" "1001"  "37011" "22009"
##   [82] "8009"  "13005" "48017" "12003" "1003"  "21007" "24510" "24005" "45009"
##   [91] "48019" "13011" "31007" "16005" "26013" "20007" "1005"  "38003" "25001"
##  [100] "45011" "21009" "55005" "13013" "26015" "18005" "20009" "13015" "48021"
##  [109] "29013" "21011" "5005"  "12005" "55007" "48023" "46005" "16007" "45013"
##  [118] "22011" "42007" "30001" "27005" "40009" "42009" "48025" "33001" "48027"
##  [127] "39013" "27007" "13017" "16009" "46007" "50003" "38005" "8011"  "53005"
##  [136] "26019" "34003" "54003" "42011" "25003" "35001" "26021" "37015" "48029"
##  [145] "13021" "22013" "56003" "27011" "38007" "16011" "19013" "18009" "37017"
##  [154] "16013" "42013" "48031" "51021" "13023" "47007" "47009" "27013" "16015"
##  [163] "28011" "29017" "46009" "17005" "16017" "16019" "29019" "48033" "48035"
##  [172] "22015" "51023" "38009" "8013"  "16021" "21017" "48037" "38011" "31013"
##  [181] "49003" "21019" "21021" "21023" "42015" "47011" "26023" "13025" "54007"
##  [190] "48039" "48041" "21025" "21027" "19017" "12009" "48043" "48045" "25005"
##  [199] "30007" "36005" "54009" "46011" "13027" "36007" "8014"  "12011" "55009"
##  [208] "46015" "37019" "40013" "29021" "51029" "42017" "19021" "31019" "21029"
##  [217] "13031" "1011"  "37021" "17011" "37023" "38015" "48051" "34005" "48053"
##  [226] "55013" "31021" "39017" "6007"  "13035" "37025" "54011" "49005" "40015"
##  [235] "22017" "6009"  "22019" "37027" "22021" "50005" "26025" "48059" "29027"
##  [244] "21035" "55015" "24009" "16025" "42021" "34007" "48061" "22023" "48063"
##  [253] "21037" "40017" "13043" "47015" "16027" "29031" "34009" "42025" "16029"
##  [262] "21039" "27017" "51033" "24013" "32510" "48065" "47019" "37031" "27019"
##  [271] "30013" "21045" "38017" "16031" "48069" "37033" "22025" "37035" "13047"
##  [280] "35003" "36009" "38019" "36011" "24015" "29039" "42027" "19033" "8015" 
##  [289] "48071" "17019" "29041" "51036" "24017" "46023" "45019" "26029" "12015"
##  [298] "13049" "31029" "13051" "13053" "13055" "36013" "35005" "47021" "26031"
##  [307] "53007" "36015" "36017" "13057" "31031" "33005" "42029" "51041" "31033"
##  [316] "28017" "5017"  "48075" "1021"  "55017" "27025" "50007" "1023"  "30015"
##  [325] "37041" "29043" "32001" "35006" "40025" "12017" "41005" "47025" "22027"
##  [334] "53009" "26035" "45027" "42031" "32003" "13059" "41007" "12019" "13063"
##  [343] "8019"  "42033" "16035" "1029"  "39025" "40027" "13065" "26037" "20029"
##  [352] "28027" "40029" "13067" "4003"  "48079" "47029" "4005"  "46029" "47031"
##  [361] "20031" "48081" "1033"  "29051" "48083" "17029" "35007" "45029" "12021"
##  [370] "48085" "48087" "48089" "13071" "13073" "39029" "37047" "6011"  "48091"
##  [379] "40031" "48095" "22029" "1035"  "8021"  "6013"  "56009" "5029"  "17031"
##  [388] "48097" "29053" "41011" "1037"  "28029" "46031" "36023" "48099" "39031"
##  [397] "8023"  "48101" "40033" "27033" "1039"  "13077" "20035" "53015" "40035"
##  [406] "5031"  "48103" "37049" "42039" "40037" "1041"  "13081" "5035"  "47033"
##  [415] "41013" "48107" "5037"  "27035" "8025"  "48109" "1043"  "51047" "23005"
##  [424] "31039" "37053" "35009" "40039" "39035" "13083" "49009" "27037" "1045" 
##  [433] "48111" "48113" "55025" "30019" "37055" "39037" "45031" "42043" "47037"
##  [442] "37059" "21059" "49011" "46035" "31045" "13085" "46037" "35011" "22031"
##  [451] "17039" "48117" "18029" "18031" "30023" "39039" "13089" "6015"  "42045"
##  [460] "26041" "29065" "48121" "8031"  "19057" "41017" "5041"  "28033" "46039"
##  [469] "40043" "48123" "48125" "51051" "38021" "26043" "47043" "45033" "48127"
##  [478] "51053" "38023" "12029" "31051" "54017" "55027" "8033"  "35013" "20043"
##  [487] "48129" "13093" "55029" "45035" "13095" "31055" "5043"  "18037" "19061"
##  [496] "49013" "25007" "31057" "29069" "55033" "17043" "37061" "37063" "36027"
##  [505] "12031" "47045" "8037"  "13099" "22033" "22035" "22037" "48133" "26045"
##  [514] "55035" "13101" "48135" "35015" "17045" "37065" "45037" "21061" "46045"
##  [523] "17047" "17049" "6017"  "8041"  "8039"  "42047" "18039" "32007" "21063"
##  [532] "48139" "20053" "1051"  "13107" "49015" "26047" "38029" "48143" "36029"
##  [541] "12033" "32009" "25009" "21065" "1055"  "32011" "22039" "13109" "51059"
##  [550] "9001"  "46047" "30025" "48145" "48147" "27043" "46049" "5045"  "51061"
##  [559] "21067" "47049" "30027" "53019" "27045" "20055" "48151" "12035" "30029"
##  [568] "21069" "45041" "18043" "51065" "48155" "55039" "20057" "55041" "28035"
##  [577] "37067" "48157" "38031" "18045" "39049" "22041" "24021" "27047" "48161"
##  [586] "8043"  "6019"  "48163" "31063" "13121" "31065" "12039" "31067" "48165"
##  [595] "30031" "39053" "48167" "31069" "8045"  "5051"  "21079" "24023" "40049"
##  [604] "48169" "29073" "37071" "37073" "20061" "39055" "16045" "26049" "1061" 
##  [613] "29075" "28039" "45043" "47053" "4007"  "12041" "47055" "48171" "41021"
##  [622] "13123" "8047"  "30035" "12043" "26051" "13125" "48173" "6021"  "34015"
##  [631] "13127" "26053" "38033" "48175" "48177" "51075" "27049" "16047" "13129"
##  [640] "56015" "31073" "20063" "40051" "33009" "4009"  "47057" "8049"  "38035"
##  [649] "50013" "26055" "30039" "53025" "22043" "37077" "26057" "21083" "48179"
##  [658] "53027" "48181" "31077" "55045" "55047" "54025" "29077" "4011"  "51081"
##  [667] "21089" "45045" "45047" "40055" "48183" "46053" "28043" "38039" "48185"
##  [676] "17063" "48187" "39059" "37081" "12045" "8051"  "19077" "13135" "46055"
##  [685] "13137" "48189" "37083" "13139" "47063" "39061" "46057" "25013" "25015"
##  [694] "51650" "45049" "18059" "46059" "51085" "48195" "46061" "13143" "12049"
##  [703] "47069" "21093" "46063" "54031" "24025" "21095" "40057" "37085" "41025"
##  [712] "20077" "48201" "28047" "21099" "9003"  "48205" "20079" "40061" "47073"
##  [721] "31085" "48209" "37087" "13149" "48211" "5057"  "48213" "18063" "12051"
##  [730] "27053" "51087" "13151" "36043" "12053" "37091" "38041" "47081" "29085"
##  [739] "48215" "39071" "12055" "48217" "12057" "26059" "28049" "8053"  "31087"
##  [748] "39073" "48219" "20083" "37093" "39075" "31089" "48221" "41027" "31091"
##  [757] "21107" "45051" "5059"  "56017" "26061" "1069"  "24027" "29091" "27057"
##  [766] "34017" "48229" "8055"  "46065" "6023"  "47085" "48231" "34019" "42061"
##  [775] "18069" "39077" "48233" "37095" "22045" "22047" "19093" "16049" "6025" 
##  [784] "5063"  "12061" "42063" "26065" "6027"  "26067" "26069" "55049" "37097"
##  [793] "48235" "49021" "17075" "13155" "26073" "27059" "53029" "51093" "28055"
##  [802] "27061" "28057" "5065"  "48237" "29095" "22049" "51095" "29097" "18075"
##  [811] "13161" "21111" "28065" "22053" "22051" "13165" "18079" "46073" "16053"
##  [820] "17083" "21113" "20089" "48247" "48249" "17085" "20091" "37101" "28067"
##  [829] "41033" "49023" "30045" "55057" "42067" "26077" "26079" "27065" "54039"
##  [838] "27067" "17089" "17091" "48255" "48257" "40071" "31099" "20093" "31101"
##  [847] "28069" "17093" "48261" "23011" "55059" "26081" "21117" "19107" "6029" 
##  [856] "48265" "45055" "55061" "26083" "31103" "38043" "31105" "48267" "51097"
##  [865] "53033" "51099" "51101" "40073" "20095" "36047" "46077" "48271" "40075"
##  [874] "8063"  "53035" "53037" "27069" "41035" "48273" "53039" "21119" "47093"
##  [883] "27071" "16055" "18085" "19109" "55063" "4012"  "8067"  "48283" "22059"
##  [892] "20099" "27073" "42069" "29105" "28071" "22055" "22057" "18087" "17097"
##  [901] "27077" "28073" "48279" "50015" "38045" "48281" "42071" "32015" "41039"
##  [910] "55067" "13173" "26087" "18091" "56021" "8069"  "21123" "8071"  "17099"
##  [919] "6035"  "16057" "40077" "1077"  "21125" "45059" "48285" "42073" "40079"
##  [928] "27079" "35025" "28079" "20103" "42075" "12071" "26089" "28083" "42077"
##  [937] "16059" "26091" "37107" "12073" "21131" "21133" "12075" "30049" "53041"
##  [946] "45063" "48291" "39089" "1083"  "37109" "22061" "19113" "48295" "9005" 
##  [955] "5081"  "48297" "26093" "22063" "48299" "39091" "13183" "5085"  "39093"
##  [964] "35028" "6037"  "47105" "51107" "51109" "31115" "40085" "48301" "13185"
##  [973] "48303" "39095" "26095" "13187" "35029" "51111" "42079" "42081" "46085"
##  [982] "48305" "32019" "26097" "26099" "17115" "17117" "6039"  "1089"  "22065"
##  [991] "21153" "19123" "27087" "39099" "40093" "41045" "12081" "26101" "55071"
## [1000] "55073" "1091"  "4013"  "29125" "6041"  "55075" "18097" "6043"  "45069"
## [1009] "26103" "1095"  "12085" "53045" "17127" "48321" "51115" "47119" "48323"
## [1018] "40097" "40087" "30055" "46087" "45065" "21145" "21147" "48307" "40089"
## [1027] "29119" "17109" "37111" "13189" "17111" "40091" "42083" "38053" "35031"
## [1036] "17113" "48309" "27085" "47107" "48311" "47109" "20113" "21163" "30059"
## [1045] "37119" "26107" "39103" "27093" "39105" "46095" "17129" "6045"  "21165"
## [1054] "26109" "6047"  "34021" "13199" "31121" "33013" "8077"  "21169" "39109"
## [1063] "12086" "25017" "48329" "42087" "48331" "49027" "27095" "29131" "19129"
## [1072] "55079" "46097" "54057" "54059" "16067" "46099" "26113" "5093"  "30063"
## [1081] "37121" "1097"  "6049"  "8081"  "4015"  "29135" "34025" "6051"  "19133"
## [1090] "54061" "36055" "48337" "26117" "6053"  "8083"  "42091" "26119" "42093"
## [1099] "8085"  "46101" "37125" "35033" "22067" "1103"  "31123" "34027" "27097"
## [1108] "39117" "38059" "48345" "17139" "38061" "27099" "21177" "41051" "13213"
## [1117] "19139" "13215" "26121" "39119" "40101" "30065" "48347" "31125" "25019"
## [1126] "6055"  "37127" "36059" "22069" "56025" "4017"  "48349" "21179" "20131"
## [1135] "20133" "28099" "20135" "6057"  "10003" "37129" "9009"  "51127" "9011" 
## [1144] "29143" "36061" "26123" "45071" "44005" "51700" "29145" "16069" "36063"
## [1153] "54067" "27103" "56027" "18113" "27105" "29147" "48353" "51710" "25021"
## [1162] "27107" "42095" "42097" "20137" "51135" "40105" "28103" "31129" "48355"
## [1171] "32023" "19141" "26125" "47131" "34029" "26127" "48357" "45073" "55083"
## [1180] "26129" "17141" "13221" "54069" "12091" "53047" "12093" "40107" "40109"
## [1189] "40111" "28105" "21185" "38065" "27109" "36065" "36067" "37133" "36069"
## [1198] "26131" "6059"  "45075" "29149" "36073" "22071" "40113" "20141" "12097"
## [1207] "26135" "36075" "35035" "31131" "36077" "26139" "27111" "5103"  "22073"
## [1216] "8091"  "55087" "47133" "18119" "21189" "16073" "23017" "29153" "55089"
## [1225] "53049" "51139" "12099" "19147" "48363" "37137" "48365" "56029" "18121"
## [1234] "48367" "48369" "12101" "37139" "34031" "51141" "13223" "40117" "16075"
## [1243] "40119" "13225" "28109" "48371" "38067" "29155" "53051" "37141" "21191"
## [1252] "46103" "23019" "17143" "55091" "46105" "37143" "42099" "32027" "37145"
## [1261] "30069" "29159" "29161" "42101" "20147" "17147" "39129" "45077" "47137"
## [1270] "53053" "21195" "4019"  "4021"  "27115" "12103" "27117" "23021" "8097" 
## [1279] "37147" "40121" "51143" "49031" "6061"  "22075" "29165" "54073" "6063" 
## [1288] "25023" "54075" "5111"  "22077" "12105" "30073" "28115" "5115"  "39133"
## [1297] "18127" "18129" "40125" "19155" "48375" "30075" "21197" "16077" "19157"
## [1306] "51145" "5117"  "20151" "39135" "28117" "48377" "26141" "54077" "55099"
## [1315] "51147" "51149" "24033" "51153" "44007" "8099"  "8101"  "5119"  "40127"
## [1324] "36079" "35037" "24035" "36081" "28119" "13241" "55101" "48379" "54081"
## [1333] "29173" "27123" "48381" "37151" "28121" "38073" "22079" "51157" "30081"
## [1342] "20153" "29177" "48383" "48385" "27125" "48387" "22081" "31145" "27127"
## [1351] "48389" "48391" "20155" "36083" "27129" "20157" "29179" "47143" "27131"
## [1360] "49033" "31147" "45079" "22083" "36085" "20161" "19159" "35039" "8103" 
## [1369] "8105"  "18137" "54085" "6065"  "47145" "51161" "46109" "47147" "37155"
## [1378] "55105" "17161" "51163" "21203" "13247" "33015" "36087" "48397" "40129"
## [1387] "40131" "38079" "20163" "35041" "26143" "27135" "30087" "39141" "8107" 
## [1396] "37159" "48399" "18139" "48401" "1113"  "47149" "50021" "48403" "22085"
## [1405] "19161" "6067"  "23023" "26145" "8109"  "34033" "5125"  "49035" "45081"
## [1414] "37163" "48405" "6069"  "6071"  "6073"  "6075"  "48407" "6077"  "35045"
## [1423] "6079"  "6081"  "35047" "48409" "48411" "46111" "30089" "35043" "39143"
## [1432] "17167" "26151" "49039" "6083"  "6085"  "6087"  "35049" "12113" "12115"
## [1441] "36091" "38081" "31153" "55111" "31155" "55113" "36093" "48413" "13249"
## [1450] "36095" "26153" "36097" "42107" "39145" "37165" "27139" "31157" "13251"
## [1459] "48415" "5129"  "5131"  "20173" "12117" "39147" "47153" "40135" "47155"
## [1468] "31159" "48417" "29203" "28125" "5135"  "6089"  "55115" "20177" "55117"
## [1477] "47157" "51171" "27141" "56033" "20181" "26155" "16079" "27143" "35051"
## [1486] "30093" "28127" "19167" "6093"  "53057" "53059" "38087" "48423" "51173"
## [1495] "53061" "42109" "35053" "6095"  "34035" "48425" "6097"  "51175" "13255"
## [1504] "45083" "21215" "46115" "53063" "51177" "22087" "29183" "22089" "17163"
## [1513] "55109" "5123"  "29187" "22091" "22093" "22095" "12109" "18141" "22097"
## [1522] "36089" "29510" "29189" "12111" "22099" "22101" "24037" "22103" "51179"
## [1531] "6099"  "46117" "37167" "31167" "39151" "18149" "48427" "29186" "27145"
## [1540] "27147" "40137" "17177" "48431" "36101" "53065" "47161" "30095" "29207"
## [1549] "37169" "29209" "48433" "32029" "19169" "33017" "38093" "56035" "51800"
## [1558] "36103" "47163" "46119" "54089" "39153" "47165" "12119" "28133" "37171"
## [1567] "42115" "10005" "6101"  "48435" "12121" "37173" "30097" "56037" "27151"
## [1576] "48437" "18155" "24041" "13265" "1121"  "28135" "1123"  "19171" "29213"
## [1585] "22105" "35055" "48439" "28137" "13267" "48441" "17179" "6103"  "13271"
## [1594] "8119"  "22107" "22109" "13273" "48445" "56039" "29215" "31169" "13275"
## [1603] "48447" "53067" "13277" "41057" "40141" "42117" "28139" "18157" "47167"
## [1612] "28141" "48449" "27153" "9013"  "48451" "36109" "49045" "30101" "13279"
## [1621] "35057" "38095" "13281" "38097" "37175" "27155" "48453" "30103" "20195"
## [1630] "55121" "13283" "21221" "21223" "48455" "46123" "13285" "47169" "39155"
## [1639] "54093" "6107"  "40143" "28143" "6109"  "46125" "1125"  "39157" "26157"
## [1648] "13289" "16083" "48457" "37177" "56041" "49047" "36111" "41059" "47171"
## [1657] "34039" "22111" "48459" "13293" "48461" "49049" "48463" "48465" "35061"
## [1666] "16085" "26159" "39161" "48467" "37181" "18163" "42121" "6111"  "17183"
## [1675] "22113" "18165" "55123" "22115" "48469" "18167" "55125" "39163" "51810"
## [1684] "12127" "18169" "27157" "20197" "27159" "40145" "53069" "37183" "12129"
## [1693] "23027" "1127"  "53071" "20199" "48473" "41063" "38099" "28147" "12131"
## [1702] "55127" "19179" "38101" "13299" "39165" "18173" "49051" "41065" "27161"
## [1711] "56043" "55129" "41067" "22117" "40149" "32031" "26161" "37189" "27165"
## [1720] "55133" "55135" "55137" "26163" "47183" "48479" "49057" "29225" "22119"
## [1729] "8123"  "18179" "22121" "22123" "22125" "36119" "42129" "56045" "54103"
## [1738] "26165" "48481" "53073" "30107" "48483" "5145"  "32033" "17195" "13313"
## [1747] "18183" "53075" "30109" "48485" "24045" "48487" "1131"  "37193" "27167"
## [1756] "28157" "17197" "48489" "39171" "45089" "48491" "47189" "9015"  "50027"
## [1765] "48495" "22127" "17201" "19191" "27169" "1133"  "54105" "48497" "21237"
## [1774] "39173" "19193" "17203" "5147"  "40151" "20207" "40153" "25027" "13321"
## [1783] "27171" "39175" "20209" "36121" "51197" "37197" "53077" "28161" "41071"
## [1792] "37199" "46135" "36123" "4025"  "28163" "5149"  "27173" "30111" "48501"
## [1801] "6113"  "42133" "48503" "6115"  "4027"  "48505" "48507" "46137" NA
data_set <- 'https://github.com/pengdsci/sta553/raw/main/data/geojson-counties-fips.json'

counties <- rjson::fromJSON(file=data_set)
#load("/Users/chengpeng/WCU/Teaching/2022Spring/STA553/RMaps/unemp.rda")
plot <- list(
      scope = 'usa',
      projection = list(type = 'albers usa'),
      showlakes = TRUE,
      lakecolor = toRGB('white')
    )
map_image <- plot_ly()  %>% 
  add_trace( type = "choropleth",
          geojson = counties,
        locations = ans$fips,
                z = ans$candidatevotes,
       colorscale = "GnBu",
       
             zmin = 0,
             zmax = 310000,
             text = ans$candidate, 
           marker = list(line=list(width=0.3))
          )   %>% 
  colorbar(title = "Votes")  %>% 
  layout( title = "Election by County 2020",
          geo = plot)
map_image
library(tidyverse)
library(knitr)
library(cowplot)
library(latex2exp)
library(plotly)
library(gapminder)
library(png)
library(RCurl)
library(gifski)
library(grDevices)
library(gganimate)
library(ggridges)
library(rjson)
## Warning: package 'rjson' was built under R version 4.0.5
library(choroplethr)
## Loading required package: acs
## Loading required package: XML
## Warning: package 'XML' was built under R version 4.0.5
## 
## Attaching package: 'acs'
## The following object is masked from 'package:htmltools':
## 
##     span
## The following object is masked from 'package:dplyr':
## 
##     combine
## The following object is masked from 'package:base':
## 
##     apply
library(choroplethrMaps)
ans
election_mapping <- ans %>% 
  select(fips,candidate) %>%
  rename(region=fips,value=candidate)

county_choropleth(election_mapping,
                  title ="2020 Election by Candidate",
                  legend = "Candidate")
## Warning in super$initialize(map.df, user.df): Your data.frame contains the
## following regions which are not mappable: NA
## Warning in self$bind(): The following regions were missing and are being set to
## NA: 1009, 1099, 27091, 27119, 16081, 27133, 19059, 27149, 27163, 28009, 28015,
## 28021, 28031, 19149, 28045, 28051, 28053, 17009, 28059, 23007, 13239, 47017,
## 47035, 47041, 47047, 47059, 47061, 27137, 23015, 23031, 28041, 31149, 31151,
## 17035, 31161, 31173, 31181, 32005, 32013, 32017, 39107, 39125, 39139, 17055,
## 39149, 47065, 47067, 47071, 47077, 47083, 47087, 17065, 47095, 47101, 47111,
## 47125, 47135, 24011, 1107, 18051, 24031, 24043, 33003, 33019, 34013, 39167,
## 40011, 40045, 47141, 48049, 48055, 48067, 48073, 48077, 24047, 18095, 34041,
## 30057, 37195, 18105, 38027, 38037, 38047, 18115, 38063, 47001, 47003, 40065,
## 40083, 40099, 18125, 18131, 30071, 30077, 30083, 30091, 38071, 38075, 38089,
## 39001, 18153, 39019, 48361, 48395, 48419, 26017, 26027, 26033, 31011, 31025,
## 31071, 31075, 39023, 39033, 39041, 39043, 35019, 35021, 47175, 47177, 45061,
## 19031, 17131, 13191, 31079, 31081, 31083, 19039, 31109, 31119, 28091, 28097,
## 28101, 2050, 17073, 28129, 17077, 36019, 36033, 36035, 36039, 17079, 41003,
## 26075, 26115, 28153, 17103, 28155, 29023, 29029, 29035, 29045, 17107, 36041,
## 36045, 36051, 36071, 17121, 48477, 48499, 49001, 49007, 26071, 26147, 29047,
## 29055, 29057, 29061, 29067, 17145, 29079, 29087, 37013, 2105, 17155, 36099,
## 36107, 36117, 42023, 45085, 17169, 46019, 46033, 46041, 46051, 17175, 27041,
## 37037, 37039, 17189, 37069, 37075, 42049, 42051, 42055, 41029, 41043, 41049,
## 18011, 27063, 29111, 29129, 29139, 29141, 18021, 29157, 29167, 37099, 37105,
## 37115, 18033, 37135, 37153, 42085, 42089, 2122, 42111, 42119, 42125, 49019,
## 48001, 13131, 29171, 29175, 29181, 29197, 29219, 30003, 13157, 39081, 39087,
## 39101, 44001, 44003, 13169, 45017, 45023, 45025, 46069, 13175, 46075, 46079,
## 46093, 46113, 46121, 46127, 30005, 30009, 30041, 13197, 31133, 31141, 19011,
## 19015, 19019, 19023, 19027, 17081, 13201, 17101, 17105, 17125, 2150, 13217,
## 13195, 13205, 13209, 13229, 13231, 13235, 13257, 13269, 20183, 20187, 20189,
## 13233, 21193, 21201, 21205, 21207, 21217, 21231, 13243, 21233, 21239, 13253,
## 21013, 21015, 17133, 17137, 17153, 17159, 17165, 17191, 18001, 13295, 13301,
## 20041, 20047, 20051, 20073, 20087, 13303, 21041, 21057, 21071, 21085, 2164,
## 20027, 21091, 21097, 21101, 48277, 48287, 48293, 48317, 48327, 20039, 48333,
## 51840, 53001, 53017, 20049, 53023, 51600, 55107, 55119, 20065, 55139, 56019,
## 54023, 54027, 54033, 20075, 54037, 54045, 54051, 54053, 54065, 48341, 54083,
## 20085, 54087, 54091, 54097, 54099, 54107, 55011, 55019, 55021, 55023, 49043,
## 20105, 50011, 56023, 51019, 20115, 51025, 51031, 51045, 51049, 51067, 2180,
## 20127, 51069, 51073, 51091, 51103, 49037, 51119, 50025, 54029, 54043, 54055,
## 20145, 54095, 51057, 21005, 21049, 21081, 21109, 40047, 37187, 38013, 38025,
## 38055, 38069, 39021, 54109, 49041, 49055, 20185, 51035, 1027, 1057, 1073, 1115,
## 1129, 21181, 19041, 19043, 20203, 19049, 19073, 19081, 19091, 19099, 19115,
## 19131, 20205, 19165, 19187, 19197, 39045, 35017, 35059, 2188, 21183, 36031,
## 36049, 51079, 13019, 21187, 13033, 13039, 13061, 2240, 6091, 8027, 20013, 20023,
## 21199, 27101, 28023, 21209, 36113, 42019, 42037, 42053, 21219, 42057, 42131,
## 1013, 1019, 21229, 2090, 12133, 13007, 13075, 26039, 45021, 47051, 47075, 1017,
## 2198, 47099, 47113, 47127, 13117, 8065, 8079, 8089, 8117, 12007, 12027, 26063,
## 26137, 26149, 27023, 47159, 45087, 46025, 46067, 1049, 1053, 1063, 21003, 1075,
## 12047, 12067, 12077, 5011, 5023, 5039, 5055, 5079, 29099, 29115, 29121, 29163,
## 29185, 29201, 29223, 30021, 46091, 46107, 46129, 40069, 40103, 40115, 5141,
## 21031, 40133, 41009, 5109, 21043, 13307, 13311, 13315, 15005, 16039, 16063,
## 17001, 21053, 17017, 30047, 31127, 31143, 31179, 31185, 34037, 41023, 41047,
## 41061, 21077, 48373, 48421, 48471, 21087, 13097, 13105, 13111, 5095, 17025,
## 17061, 17069, 18049, 18053, 18093, 18099, 21103, 18133, 18145, 30053, 30085,
## 31001, 21115, 31017, 31027, 31041, 31059, 48493, 21121, 49017, 47187, 21137,
## 2100, 2170, 1105, 1111, 21143, 19003, 19025, 19035, 17071, 17087, 17095, 21151,
## 17123, 17135, 17151, 17187, 17199, 31107, 28077, 28081, 28085, 21157, 28087,
## 28107, 28123, 28145, 28159, 29015, 29049, 29059, 29071, 48119, 48131, 48153,
## 21175, 48241, 19045, 48269, 48289, 6105, 19047, 18017, 18027, 18041, 13133,
## 13153, 19055, 13163, 13193, 13207, 13219, 13227, 13237, 29083, 29093, 19063,
## 37045, 19067, 37123, 48313, 48325, 48335, 48343, 51133, 19079, 51167, 51515,
## 51590, 51620, 51680, 51720, 51740, 51830, 19087, 12123, 13259, 13297, 20025,
## 20033, 20045, 19095, 20069, 20081, 20101, 20111, 20123, 20139, 19105, 20159,
## 20169, 37157, 37179, 39069, 39083, 39115, 39127, 39137, 53011, 19121, 55053,
## 55078, 56005, 19125, 2016, 2060, 20179, 20191, 19137, 21227, 21235, 48105,
## 19151, 48193, 48351, 51137, 51169, 51181, 51183, 55037, 55043, 55055, 19175,
## 53043, 48197, 48199, 19181, 48203, 48223, 48239, 48243, 19195, 48253, 48259,
## 51185, 51191, 51195, 51520, 51550, 51580, 51610, 51640, 51678, 51683, 51685,
## 51730, 51750, 51790, 55065, 20015, 55077, 55085, 55093, 55097, 54005, 54015,
## 54019, 12037, 12065, 8093, 27083, 8113, 8115, 5139, 2290, 18171, 18177, 6033,
## 17059, 17067, 18061, 18073, 18081, 18083, 18089, 18107, 20121, 20129, 20175,
## 5021, 21127, 21141, 21149, 21155, 19051, 19065, 27113, 19071, 19083, 19085,
## 19097, 18111, 19119, 19143, 27121, 19173, 19185, 20001, 20011, 27075, 21161,
## 28001, 28019, 28025, 19117, 37079, 37089, 42035, 42041, 20021, 28063, 38049,
## 28111, 28113, 28131, 42059, 42065, 28013, 47023, 38051, 38057, 40067, 40095,
## 36025, 40139, 41001, 41041, 31139, 19029, 19037, 17119, 47039, 28037, 23013,
## 23025, 25025, 41015, 26105, 42127, 28061, 49025, 31165, 31175, 31177, 30067,
## 30079, 28149, 29005, 28075, 29011, 29025, 39085, 44009, 39111, 39113, 39123,
## 30105, 38077, 38085, 38105, 29037, 36053, 41055, 23009, 29101, 29107, 29113,
## 29123, 29127, 23029, 47079, 47089, 47091, 47097, 47103, 47115, 47121, 47123,
## 24001, 48443, 29199, 31015, 46071, 46081, 24019, 46083, 46089, 33007, 33011,
## 24029, 34011, 31043, 31049, 31061, 39051, 29063, 29081, 29089, 37029, 29133,
## 37113, 24039, 40001, 40007, 40021, 40023, 40041, 35023, 47181, 45057, 25011,
## 36105, 36115, 45091, 46013, 46021, 29217, 29227, 39067, 39079, 48057, 48093,
## 45067, 46027, 27015, 27021, 27027, 30011, 30017, 30033, 34023, 31095, 31111,
## 28089, 28093, 27031, 27055, 37051, 37131, 37149, 42103, 30061, 38001, 10001,
## 5069, 5075, 5077, 5007, 16087, 17013, 17021, 17027, 16037, 18023, 2282, 15003,
## 5105, 5107, 18123, 18143, 18147, 26085, 13145, 13167, 13177, 13179, 20107,
## 20117, 17149, 17157, 17171, 17173, 17181, 17185, 17193, 13287, 13291, 21105,
## 48315, 54047, 54063, 54071, 51071, 51077, 51083, 51089, 51105, 21033, 13305,
## 51117, 26111, 48141, 51131, 51159, 51165, 48225, 48245, 51193, 51199, 53031,
## 55031, 55051, 51570, 26133, 51630, 51735, 51770, 53055, 31113, 37043, 39057,
## 48207, 51690, 55131, 1109, 5049, 5127, 38083, 41019, 55001, 18047, 37185, 45015,
## 27009, 13045, 20149, 42001, 37161, 21051, 13245, 13261, 13263, 20193, 20201,
## 21211, 21213, 21225, 27029, 20037, 20067, 20071, 27039, 49053, 27051, 50009,
## 50017, 50023, 21047, 21055, 21073, 21075, 56001, 56007, 56011, 56013, 54035,
## 54041, 51027, 51037, 29103, 29109, 29117, 29137, 29151, 29169, 29195, 29205,
## 29211, 29221, 29229, 30037, 30043, 31135, 31137, 31163, 31171, 31183, 32021,
## 30051, 30099, 31009, 31023, 31035, 31037, 8035, 31047, 31053, 31093, 31097,
## 31117, 28095, 28151, 29001, 29009, 12083, 29033, 37005, 12087, 37057, 37103,
## 37117, 12095, 39063, 39065, 39097, 39121, 39131, 39159, 39169, 40019, 40053,
## 37191, 12125, 38091, 38103, 39003, 39015, 39027, 39047, 35027, 36021, 36037,
## 36057, 13029, 42105, 42113, 42123, 45039, 47005, 47013, 47027, 47117, 47129,
## 47139, 47151, 47173, 47179, 45053, 46017, 46043, 40059, 40063, 13079, 40081,
## 40123, 40147, 13091, 41031, 41037, 41053, 41069, 48359, 48393, 48429, 13113,
## 48475, 47185, 48011, 48047, 48115, 48137, 48149, 48159, 48191, 8057, 48227,
## 48251, 48263, 48275, 8059, 48319, 48339, 51121, 51125, 8073, 51155, 51187,
## 51510, 51530, 51540, 51595, 51660, 51670, 51760, 51775, 51820, 53013, 53021,
## 8095, 55069, 55081, 55095, 55103, 55141, 56031, 54001, 8111, 54013, 54021,
## 54049, 54079, 54101, 55003, 49029, 50019, 51017, 51043, 51063, 51113, 1025,
## 1031, 1047, 1059, 1067, 1071, 12013, 1081, 10